﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 3.1
{
    class Program
    {
        static void Main(string[] args)
        {
            LinkNode<int> node = new LinkNode<int>(0);
            LinkList<int> link1 = new LinkList<int>();
            link1.First = node;
            Console.WriteLine("测试是否为空：");
            if (link1.IsEmpty())
                Console.WriteLine("链表为空！");
            else Console.WriteLine("链表非空！");
            Console.WriteLine("创建一个5节的链表：");

            LinkNode<int> q = link1.First;
            Random random = new Random(GetRandomSeedByGuid());
            for (int i = 0; i < 5; i++)
            {
                LinkNode<int> AddNode = new LinkNode<int>(random.Next(1, 10));
                q.next = AddNode;
                q = AddNode;
            }
            link1.printf();
            Console.WriteLine("设置元素为1的测试节点");
            node = new LinkNode<int>(1);
            Console.WriteLine("在链表第2个位置索引测试节点后：");
            link1.Suoyin(2, node);
            link1.printf();
            Console.WriteLine("查找元素为1的节点后：");
            link1.IndexOf(1);
            link1.printf();
            Console.WriteLine("在链表第4个位置插入测试节点后：");
            link1.InsertAt(4, node);
            link1.printf();
            Console.WriteLine("删除链表第2个位置元素后：");
            link1.RemoveAt(2);
            link1.printf();
            Console.ReadKey();

        }
        static int GetRandomSeedByGuid()//随机数种子
        {
            return Guid.NewGuid().GetHashCode();
        }
    }

    class LinkNode<T>
    {
        public T Date;
        public LinkNode<T> next;
        public LinkNode(T data)
        {
            Date = data;
            next = null;
        }
    }
    public interface ILinkable<T>
    {
    }
    class LinkList<T> : ILinkable<T>
    {
        private LinkNode<T> first;
        public LinkNode<T> First
        {
            get { return first; }
            set { first = value; }
        }
        public bool IsEmpty()
        {
            if (first.next == null)
                return true;
            else return false;
        }
        private int count;
        public int Count
        {
            get
            {
                int i = 0; LinkNode<T> node = first;
                while (node.next != null)
                {
                    node = node.next;
                    i++;
                }
                return i;
            }
        }
        public LinkNode<T> Getnode(int pos)
        {
            if (pos <= Count && pos > 0)
            {
                LinkNode<T> node = first;
                for (int i = 0; i < pos; i++)
                {

                    node = node.next;
                }
                return node;
            }
            else { Console.WriteLine("参数错误！"); return null; }
        }
        public void Suoyin(int pos, LinkNode<T> Newnode)
        {
            if (pos <= Count && pos > 0)
            {
                LinkNode<T> node = first;
                for (int i = 0; i < pos; i++)
                {
                    if (node.next == null)
                        throw new IndexOutOfRangeException();
                    node = node.next;
                }
                node.Date = Newnode.Date;
            }
            else Console.WriteLine("参数错误！");

        }//索引
        public bool IndexOf(T date)
        {
            int i = 0, index = 0;
            int[] a = new int[10];
            LinkNode<T> node = first;
            while (node.next != null)
            {
                i++;
                node = node.next;
                if (node.Date.Equals(date))
                {
                    a[index++] = i;

                }
            }
            if (a[0] > 0)
            {
                for (i = 0; i < index; i++)
                    Console.WriteLine("找到date元素在链表的第{0}个位置", a[i]);
                return true;
            }
            else return false;
        }//查找
        public void InsertAt(int pos, LinkNode<T> Newnode)
        {
            if (pos <= Count && pos > 0)
            {
                LinkNode<T> node = first;
                LinkNode<T> q = null;
                for (int i = 0; i < pos; i++)
                {

                    q = node;
                    node = node.next;
                }
                q.next = Newnode;
                Newnode.next = node;
            }
            else Console.WriteLine("参数错误！");
        }//插入
        public void RemoveAt(int pos)
        {
            if (pos <= Count && pos > 0)
            {
                LinkNode<T> node = first;
                LinkNode<T> q = null;
                for (int i = 0; i < pos; i++)
                {

                    q = node;
                    node = node.next;
                }
                q.next = node.next;
            }
            else Console.WriteLine("参数错误！");
        }//删除
        public void printf()
        {
            LinkNode<T> node = first;
            Console.WriteLine("遍历链表：");
            while (node.next != null)
            {
                node = node.next;
                Console.Write("{0}   ", node.Date);
            }
            Console.WriteLine("共{0}个元素", Count);
        }//遍历
    }






